package com.stylefeng.guns.modular.system.api;

import com.alibaba.druid.util.StringUtils;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.response.JsonResult;
import com.stylefeng.guns.core.response.ResultGenerator;
import com.stylefeng.guns.core.util.*;
import com.stylefeng.guns.modular.system.model.PhoneUserinfoRepeat;
import com.stylefeng.guns.modular.system.model.Userinfo;
import com.stylefeng.guns.modular.system.service.IPhoneUserinfoRepeatService;
import com.stylefeng.guns.modular.system.service.IUserinfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api")
public class UserInfoPhoneApiController extends BaseController {
    @Autowired
    IUserinfoService iUserinfoService;

    @Autowired
    IPhoneUserinfoRepeatService phoneUserinfoRepeatService;

    @Value("${adv.ipAddr.location}")
    private String location;

    @Autowired
    RedisUtil redisUtil;

    @RequestMapping(value = "/phone")
    @Transactional
    public JsonResult addPhoneUserinfo(HttpServletRequest request){

        Userinfo userinfo = null;
        PhoneUserinfoRepeat phoneUserinfoRepeat = null;
        String ip = null;
        List<PhoneUserinfoRepeat> prepeat = null;
        List<Userinfo> puinfo = null;
        String dizhiArr [] = null;
        try {
            ip = NetworkUtil.getIpAddress(request);
            userinfo = getPhoneUserInfo(request);

            if(userinfo==null){
                return ResultGenerator.genFailResult("userinfo is null");
            }

            if("127.0.0.1".equals(ip)) {
                userinfo.setReqip(ip);
                userinfo.setPhoneCountry("局域网");
                userinfo.setPhoneCity("局域网");
                userinfo.setPhoneProvince("局域网");
            }else{
                dizhiArr = IpUtil.getAddress(ip,location);
                if(dizhiArr!=null) {
                    userinfo.setReqip(ip);
                    userinfo.setPhoneCountry(dizhiArr[0]);
                    userinfo.setPhoneCity(dizhiArr[3]);
                    userinfo.setPhoneProvince(dizhiArr[2]);
                }else{
                    userinfo.setReqip(ip);
                    userinfo.setPhoneCountry("局域网");
                    userinfo.setPhoneCity("局域网");
                    userinfo.setPhoneProvince("局域网");
                }
            }

            phoneUserinfoRepeat = new PhoneUserinfoRepeat();

            BeanUtils.copyProperties(userinfo,phoneUserinfoRepeat);//复制一份
            //---------------------------
            //第一次进来uuid，加到主表中
            //第二次进来，如果不同包，加入到重复表中
            //相同包，则直接返回数据，不添加任何
            //----------------------------

            if(phoneUserinfoRepeat==null){
                return ResultGenerator.genFailResult("phoneUserinfoRepeat is null");
            }

            if(userinfo==null){
                return ResultGenerator.genFailResult("userinfo is null");
            }

            if(StringUtils.isEmpty(userinfo.getPhoneUdid())||"".equals(userinfo.getPhoneUdid())){
                return ResultGenerator.genFailResult("phoneUdid is null");
            }

            //进行键值对的判断
            if(redisUtil.hasKey(userinfo.getPhoneUdid())){
                if(redisUtil.hasKey(userinfo.getPhoneUdid()+userinfo.getPhoneSoftname())){
                    //直接返回
                    return ResultGenerator.genSuccessResult();
                }else {
                    prepeat = phoneUserinfoRepeatService.selByPhoneUdidAndPhoneSoftname(phoneUserinfoRepeat);
                    if(prepeat==null||prepeat.size()==0) {
                        //放到重复表中去
                        phoneUserinfoRepeatService.addPhoneUserinfoRepeat(phoneUserinfoRepeat);
                    }
                }
            }else {
                //查询数据库中是否有数据
                puinfo = iUserinfoService.selByPhoneUdid(userinfo.getPhoneUdid());
                if(puinfo==null||puinfo.size()==0) {
                    //不重复，就在新表中
                    iUserinfoService.addPhoneUserinfo(userinfo);
                }else{
                    if(puinfo.get(0).getPhoneSoftname().equals(userinfo.getPhoneSoftname())){
                        //直接返回
                        return ResultGenerator.genSuccessResult();
                    }else{
                        if(!redisUtil.hasKey(userinfo.getPhoneUdid()+userinfo.getPhoneSoftname())) {
                            prepeat = phoneUserinfoRepeatService.selByPhoneUdidAndPhoneSoftname(phoneUserinfoRepeat);
                            if (prepeat == null || prepeat.size() == 0) {
                                //放到重复表中去
                                phoneUserinfoRepeatService.addPhoneUserinfoRepeat(phoneUserinfoRepeat);
                            }
                        }
                    }
                }
                redisUtil.set(userinfo.getPhoneUdid(), userinfo);//加缓存
                redisUtil.set(userinfo.getPhoneUdid()+userinfo.getPhoneSoftname(),userinfo);//加缓存
            }
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult(e.getMessage());
        }finally {
            userinfo = null;
            phoneUserinfoRepeat = null;
            prepeat = null;
            puinfo = null;
            dizhiArr = null;
        }

    }

    /**
     * 获取用户信息
     * */
    public Userinfo getPhoneUserInfo(HttpServletRequest request){
        Userinfo phoneUserinfo = new Userinfo();
        try {
            phoneUserinfo.setCurrentnetworktype(request.getParameter("currentnetworktype"));
            phoneUserinfo.setCpid(request.getParameter("cpid"));
            phoneUserinfo.setPhoneSoftname(request.getParameter("phoneSoftname"));
            phoneUserinfo.setPhoneSoftversion(request.getParameter("phoneSoftversion"));
            phoneUserinfo.setType(request.getParameter("type"));
            phoneUserinfo.setPhoneImsi(request.getParameter("phoneImsi"));
            phoneUserinfo.setPhoneImei(request.getParameter("phoneImei"));
            phoneUserinfo.setPhoneMac(request.getParameter("phoneMac"));
            phoneUserinfo.setPhoneApn(request.getParameter("phoneApn"));
            phoneUserinfo.setPhoneFirmwareVersion(request.getParameter("phoneFirmwareVersion"));
            phoneUserinfo.setPhoneBrand(request.getParameter("phoneBrand"));
            phoneUserinfo.setPhoneModel(request.getParameter("phoneModel"));
            phoneUserinfo.setAddtime(DateUtils.getDateTime());
            phoneUserinfo.setSid(request.getParameter("sid"));
            phoneUserinfo.setCoreSdkVersion(request.getParameter("coreSdkVersion"));
            phoneUserinfo.setCodeLocation(request.getParameter("codeLocation"));
            phoneUserinfo.setAndroidid(request.getParameter("androidid"));
            phoneUserinfo.setOs(request.getParameter("os"));
            phoneUserinfo.setPhoneResolution(request.getParameter("phoneResolution"));
            phoneUserinfo.setPhoneUdid(request.getParameter("phoneUdid"));
            phoneUserinfo.setAppkey(request.getParameter("appkey"));
            phoneUserinfo.setSdkVersion(request.getParameter("sdkVersion"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return phoneUserinfo;
    }
}
